package com.frank.yinyuetai.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.frank.yinyuetai.Bean.VideoBean;
import com.frank.yinyuetai.MVDetailActivity;
import com.frank.yinyuetai.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/8/18 0018.
 */
public class MVRecycleViewAdapter extends RecyclerView.Adapter<MVRecycleViewAdapter.ViewHolder> {
    private List<VideoBean> videoList;
    private Context context;
    private RelativeLayout.LayoutParams layoutParams;

/*    public MVRecycleViewAdapter(List<VideoBean> videoList, Context context, int mWidth, int mHeight) {
        this.videoList = videoList;
        this.context = context;
        layoutParams = new RelativeLayout.LayoutParams(mWidth, mHeight);
    }*/

    public MVRecycleViewAdapter(List<VideoBean> videoList, Context context) {
        this.videoList = videoList;
        this.context = context;

    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mv_recycleview_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final VideoBean videoBean = videoList.get(position);
        Log.i("dfjkljk", videoList.toString());
       /* holder.posterImg.setLayoutParams(layoutParams);
        holder.itemTransbg.setLayoutParams(layoutParams);*/
        holder.name.setText(videoBean.getTitle());
        holder.author.setText(videoBean.getDescription());
        if (videoBean.isAd()) {
            Glide.with(context).
                    load(videoBean.getThumbnailPic()).
                    diskCacheStrategy(DiskCacheStrategy.ALL).
                    into(holder.posterImg);
            holder.playCount.setText("");
        } else {
            Glide.with(context).load(videoBean.getAlbumImg()).diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.posterImg);
            holder.playCount.setText("播放次数：" + videoBean.getTotalViews());
            holder.itemRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(context, MVDetailActivity.class);
                    intent.putExtra("id", videoBean.getId());
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return videoList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.empty_logo)
        ImageView emptyLogo;
        @Bind(R.id.poster_img)
        ImageView posterImg;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.author)
        TextView author;
        @Bind(R.id.play_count)
        TextView playCount;
        @Bind(R.id.item_transbg)
        ImageView itemTransbg;
        @Bind(R.id.item_root)
        RelativeLayout itemRoot;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
